var vm;
var _UrlPrefix = "http://www.fuzhuangchangongxiao.cn/";
var _AppAccess = web_access;

(function ($) {
    var staff = {
        init: function () {
            vm = new Vue({
                el: "#Staff",
                // 数据
                data: {
                    manageDataFactory: [],
                    manageDataCompany: [],
                    curPostFirst: {},
                    curPostSecond: {},
                    panelLeft: false,
                    panelRight: false,
                    pages: ['EmployeeType', 'EmployeeSelect', 'BasicInfo1', 'BasicInfo2', 'Objective', 'Certification'],
                    curPage: 'EmployeeType',
                    curType: null,
                    kind: ''
                },

                // 周期
                mounted: function () {},

                // 侦听器
                watch: {},

                // 计算属性
                computed: {
                    selectFactory: function () {
                        if(this.manageDataFactory.filter((item) => item.id == this.kind).length > 0) {
                            var _data = this.manageDataFactory.filter((item) => item.id == this.kind)[0]
                            this.curPostSecond = _data.son[0]
                            return _data
                        }
                    },
                    selectCompany: function () {
                        if(this.manageDataCompany.filter((item) => item.id == this.kind).length > 0) {
                            var _data = this.manageDataCompany.filter((item) => item.id == this.kind)[0]
                            this.curPostSecond = _data.son[0]
                            return _data
                        }
                    }
                  },

                // 方法
                methods: {
                    onSelectType: function (_value) {
                        var self = this, _type;
                        if("Factory" == _value) {
                            _type = 1
                        }else {
                            _type = 2
                        }
                        $.ajax({
                            type:"POST",
                            url: _UrlPrefix + "api/web/personal/set-type",
                            data: {
                                cate: _type,
                                app_access: _AppAccess
                            },
                            success:function(res){
                                if(res.code == 200) {
                                    self.onGetInfo(_type)
                                    for(var i = 0;i < self.pages.length;i++) {
                                        if(self.curPage.indexOf(self.pages[i]) > -1) {
                                            self.curPage = _value+self.pages[i+1]
                                            return
                                        }
                                    }
                                }else if(res.code == 30002) { console.log(res.data) }
                                else { console.log(res) }
                            },
                            error: function(xhr,status,error) {},
                            complete: function () {}
                        })
                    },
                    onGetInfo: function(_type) {
                        // 获取员工信息
                        var self = this
                        // 职位选择1、工厂 2、公司
                        $.ajax({
                            type:"GET",
                            url: _UrlPrefix + "api/web/common/getJobs",
                            data: {
                                cate: _type,
                                app_access: _AppAccess
                            },
                            success:function(res){
                                if(res.code == 200) {
                                    console.log("data: ", res.data)
                                    _type == 1 ? self.manageDataFactory = res.data : self.manageDataCompany = res.data
                                }else{
                                    console.log(res.data)
                                }
                            },
                            error: function(xhr,status,error) {},
                            complete: function () {}
                        })
                    },
                    onSelectJob: function(e) {
                        this.curPostSecond.id = e.target.getAttribute("data-id")
                        this.curPostSecond.pid = e.target.getAttribute("data-pid")
                        this.curPostSecond.name = e.target.getAttribute("data-name")
                    },
                    onSettingWorker: function() {
                        var self = this
                        // 设置职位信息
                        $.ajax({
                            type:"POST",
                            url: _UrlPrefix + "api/web/common/setJob",
                            data: {
                                uid: self.curPostSecond.pid,
                                job: self.curPostSecond.id,
                                app_access: _AppAccess
                            },
                            success:function(res){
                                if(res.code == 200) {
                                    console.log("职位设置成功：", res.data)
                                }else if(res.code == 30002) {
                                    console.log(res.data)
                                }else {
                                    console.log(res)
                                }
                            },
                            error: function(xhr,status,error) {},
                            complete: function () {}
                        })
                    },
                    onNextStep: function (_type) {
                        // 下一步
                        if("factory" == _type) {
                            this.onSettingWorker()
                        }else if("manage" == _type) {
                            this.onSettingWorker()
                        }else if("basic1" == _type) {
                            this.onSettingWorker()
                        }else if("basic2" == _type) {
                            this.onSettingWorker()
                        }else if("factoryObjective" == _type) {
                            this.onSettingWorker()
                        }else if("companyObjective" == _type) {
                            this.onSettingWorker()
                        }else if("certification" == _type) {
                            this.onSettingWorker()
                        }
                        
                        for(var i = 0;i < this.pages.length-1;i++) {
                            if(this.curPage.indexOf(this.pages[i]) > -1) {
                                this.curPage = this.curType+this.pages[i+1]
                                return
                            }
                        }
                        console.log("注册成功")
                    },
                    mBtnEnter: function (e) {
                        this.kind = e.target.getAttribute("data-id")
                        var _id = parseInt(e.target.getAttribute("data-k"))
                        this.curPostFirst = e.target.getAttribute("name")

                        if(_id % 2 == 0) {
                            this.panelLeft = true;
                            this.panelRight = false;
                        }else {
                            this.panelLeft = false;
                            this.panelRight = true;
                        }
                    },
                    mBtnLeave: function () {
                        let self = this;
                        self._timer = setTimeout(function () {
                          self.kind = ''
                        }, 20)
                    },
                    mPanlOver: function () {
                        clearTimeout(this._timer)
                    },
                    mPanlOut: function () {
                        this.kind = ''
                    }
                },

                // 周期
                created: function () {},
            })
        }
    }
    staff.init()
})(jQuery)